/**
 * Copyright @2012 中国电信甘肃万维公司 All rights reserved.
 * 中国电信甘肃万维公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 * 日期 2012-7-26 上午11:10:32
 */
package com.oa.cs.agreement.parser.impl;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.oa.cs.agreement.parser.AgreementParser;
import com.oa.cs.agreement.pojo.ResponseObject;

/**
 * <p>
 * AgreementJsonParser描述:
 * </p>
 * @company 中国电信甘肃万维公司 
 *
 * @project nma-s-agreement
 *
 * @version V2.0.0
 *
 * @author 郭磊(产品部)
 *
 * @date 2012-7-26 上午11:10:32	
 *
 * @class com.oa.s.agreement.parser.AgreementJsonParser
 *
 */
public class AgreementJsonParser implements AgreementParser
{
	/** 
	 * @Fields serialVersionUID :  TODO(用一句换描述这个变量的作用)
	 */
	private static final long serialVersionUID = -620100952425649070L;
	
	@Override
	public String responseToString(ResponseObject response)
	{
		ObjectMapper mapper = new ObjectMapper();
		try
		{
			return mapper.writeValueAsString(response);
		}
		catch (JsonGenerationException e)
		{
			e.printStackTrace();
		}
		catch (JsonMappingException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return EMPTY_JSON;
	}

	@Override
	public ResponseObject stringToResponse(String string)
	{
		ObjectMapper mapper = new ObjectMapper();
		try
		{
			ResponseObject response = mapper.readValue(string,new TypeReference<ResponseObject>(){});
			return response;
		}
		catch (JsonParseException e)
		{
			e.printStackTrace();
		}
		catch (JsonMappingException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * TODO将对象转换为json字符串
	 * @param obj
	 * @return
	 */
	public String objectToString(Object obj)
	{
		ObjectMapper mapper = new ObjectMapper();
		try
		{
			return mapper.writeValueAsString(obj);
		}
		catch (JsonGenerationException e)
		{
			e.printStackTrace();
		}
		catch (JsonMappingException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return EMPTY_STR;
	}

	@Override
	public Object stringToObject(String obj , Class cls)
	{
		ObjectMapper mapper = new ObjectMapper();
		try
		{
			return mapper.readValue(obj, cls);
		}
		catch (JsonGenerationException e)
		{
			e.printStackTrace();
		}
		catch (JsonMappingException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return EMPTY_STR;
	}
}
